package com.aaa.service.system.impl;

import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.lang.tree.TreeNodeConfig;
import cn.hutool.core.lang.tree.TreeUtil;
import com.aaa.entity.system.Menu;
import com.aaa.dao.system.MenuDao;
import com.aaa.service.system.MenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 菜单权限表(Menu)表服务实现类
 *
 * @author makejava
 * @since 2022-08-30 08:26:06
 */
@Service("menuService")
public class MenuServiceImpl implements MenuService {
    @Autowired
    private MenuDao menuDao;

    /**
     * 查询所有数据并包装成树形结构
     *
     * @return 实例对象
     */
    @Override
    public List<Tree<String>> queryAllMenu() {

        List<Menu> menus = menuDao.queryAllMenu();
        //配置
        TreeNodeConfig treeNodeConfig = new TreeNodeConfig();
        // 自定义属性名 都要默认值的
        treeNodeConfig.setWeightKey("menuType");
        treeNodeConfig.setIdKey("menuId");
        treeNodeConfig.setNameKey("menuName");
        treeNodeConfig.setParentIdKey("parentId");

        // 最大递归深度
        treeNodeConfig.setDeep(3);

        //转换器
        List<Tree<String>> treeNodes = TreeUtil.build(menus, "0", treeNodeConfig,
                (treeNode, tree) -> {
                    tree.setId(treeNode.getMenuId()+"");
                    tree.setParentId(treeNode.getParentId()+"");
                    tree.setWeight(treeNode.getMenuType());
                    tree.setName(treeNode.getMenuName());
                    tree.putExtra("orderNum",treeNode.getOrderNum());
                    tree.putExtra("url",treeNode.getUrl());
                    tree.putExtra("perms",treeNode.getPerms());
                    tree.putExtra("remark",treeNode.getRemark());
                    tree.putExtra("createTime",treeNode.getCreateTime());
                });
        return treeNodes;

    }

    @Override
    public List<Tree<String>> queryAllMenus() {
        List<Menu> menus = menuDao.queryAllMenu();
        List<Map<String,Object>> mapList = new ArrayList<>();
        Map<String,Object> map =null;
        for (Menu menu : menus) {
            map =  new HashMap<>();
            map.put("title",menu.getMenuName());
            map.put("menuType",menu.getMenuType());
            map.put("menuId",menu.getMenuId());
            map.put("parentId",menu.getParentId());
            mapList.add(map);
        }

        //配置
        TreeNodeConfig treeNodeConfig = new TreeNodeConfig();
        // 自定义属性名 都要默认值的
        treeNodeConfig.setWeightKey("menuType");
        treeNodeConfig.setIdKey("menuId");
        treeNodeConfig.setParentIdKey("parentId");
        treeNodeConfig.setNameKey("title");

        // 最大递归深度
        treeNodeConfig.setDeep(3);

        //转换器
        List<Tree<String>> treeNodes = TreeUtil.build(mapList, "0", treeNodeConfig,
                (treeNode, tree) -> {
                    tree.setId(treeNode.get("menuId")+"");
                    tree.setParentId(treeNode.get("parentId")+"");
                    tree.setWeight(treeNode.get("menuType")+"");
                    tree.setName(treeNode.get("title")+"");

                });
        return treeNodes;
    }

    @Override
    public Map<String, Object> queryMenuByUsername(String username) {
        Map<String, Object> map = new HashMap();

        List<Menu> menuList = menuDao.queryMenuByUsername(username);
        List<Menu> firstMenuList = new ArrayList<>();
        List<Menu> secondMenuList = new ArrayList<>();
        //菜单分类
        for (Menu menu : menuList) {
            if (menu.getMenuType().equals("M")) {
                //一级菜单
                firstMenuList.add(menu);
            }
            if (menu.getMenuType().equals("C")) {
                //二级菜单
                secondMenuList.add(menu);
            }
        }
        map.put("firstMenuList", firstMenuList);
        map.put("secondMenuList", secondMenuList);
        return map;
    }


    /**
     * 新增数据
     *
     * @param menu 实例对象
     * @return 实例对象
     */
    @Override
    public boolean insertMenu(Menu menu) {
        return menuDao.insertMenu(menu);
    }

    /**
     * 修改数据
     *
     * @param menu 实例对象
     * @return 实例对象
     */
    @Override
    public boolean updateMenu(Menu menu) {
        return menuDao.updateMenu(menu);
    }

    @Override
    public boolean deleteMenuById(Integer menuId) {
        return menuDao.deleteMenuById(menuId);
    }

    @Override
    public List<Menu> findParentNameId(Integer menuId) {
        return menuDao.findParentNameId(menuId);
    }


}
